﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TetrisAlgorithm;

namespace TetrisProject
{
    public partial class frmTetris : Form
    {
        FigureInfo fInfo = new FigureInfo();
        FigureMove fMove = new FigureMove();
        AssemblingFigureInfo asInfo = new AssemblingFigureInfo();
        List<Point> listPoint = new List<Point>();
        List<FigureInfo> listInfo = new List<FigureInfo>();
        public frmTetris()
        {
            InitializeComponent();
            this.KeyPreview = true;
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Left)
            {
                //fMove.MoveLeft();
                //fInfo = fMove.figureInfo;
                //fInfo.Location = fInfo.figurePoint;
                MoveLeft();
            }
            else if (keyData == Keys.Right)
            {
                //fMove.MoveRight();
                //fInfo = fMove.figureInfo;
                //fInfo.Location = fInfo.figurePoint;
                MoveRight();
            }
            return true;
        }

        private void frmTetris_Load(object sender, EventArgs e)
        {
            this.timer1.Interval = 200;
          //  GetNewSqure();
          //  GetNewRectangle();
           // GetNewAssemblingOne();
           // GetNewAssemblingTwo();
           // GetNewAssemblingThrea();

            RandomNew();
        }
        /// <summary>
        /// TimeTick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            timeRefresh();
        }
        /// <summary>
        /// 开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBegain_Click(object sender, EventArgs e)
        {
            timeStart();
        }
        /// <summary>
        /// 结束
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEnd_Click(object sender, EventArgs e)
        {
            timeStop();
            this.Close();
        }
        /// <summary>
        /// 到点刷新
        /// </summary>
        private void timeRefresh()
        {
            fMove.listInfo = listInfo;
            List<Point> listLocation = fMove.MoveDown();
            if (listLocation != null)
            {
                for (int i = 0; i < listLocation.Count; i++)
                {
                    listInfo[i].figurePoint = listLocation[i];
                    listInfo[i].Location = listLocation[i];
                }
            }
            else//生成新图形
            {
                foreach (FigureInfo item in listInfo)
                {
                    fMove.listAccumulateInfo.Add(item);
                }
                RandomNew();
            }
        }
        /// <summary>
        /// 开始
        /// </summary>
        private void timeStart()
        {
            if (fInfo != null)
            {
               // fMove.figureInfo = fInfo;
                fMove.maxPoint = new Point(250, 250);
                this.timer1.Start();
            }
        }
        /// <summary>
        /// 暂停。结束
        /// </summary>
        private void timeStop()
        {
            this.timer1.Stop();
        }
        /// <summary>
        /// 正方形
        /// </summary>
        private void GetNewSqure()
        {
            listInfo = asInfo.GetSqureInfo();
            listPoint = asInfo.GetSqurePoint();
            for (int i = 0; i < listInfo.Count; i++)
            {
                FigureInfo figure = listInfo[i];
                figure.Location = listPoint[i];
                this.panMain.Controls.Add(figure);
            }
        }
        /// <summary>
        /// 长方形
        /// </summary>
        private void GetNewRectangle()
        {
            listInfo = asInfo.GetRectangleInfo();
            listPoint = asInfo.GetRectanglePoint();
            for (int i = 0; i < listInfo.Count; i++)
            {
                FigureInfo figure = listInfo[i];
                figure.Location = listPoint[i];
                this.panMain.Controls.Add(figure);
            }
        }
        /// <summary>
        /// 组合1
        /// </summary>
        private void GetNewAssemblingOne()
        {
            listInfo = asInfo.GetAssemblingOneInfo();
            listPoint = asInfo.GetAssemblingOnePoint();
            for (int i = 0; i < listInfo.Count; i++)
            {
                FigureInfo figure = listInfo[i];
                figure.Location = listPoint[i];
                this.panMain.Controls.Add(figure);
            }
        }
        /// <summary>
        /// 组合2
        /// </summary>
        private void GetNewAssemblingTwo()
        {
            listInfo = asInfo.GetAssemblingTwoInfo();
            listPoint = asInfo.GetAssemblingTwoPoint();
            for (int i = 0; i < listInfo.Count; i++)
            {
                FigureInfo figure = listInfo[i];
                figure.Location = listPoint[i];
                this.panMain.Controls.Add(figure);
            }
        }
        /// <summary>
        /// 组合3
        /// </summary>
        private void GetNewAssemblingThrea()
        {
            listInfo = asInfo.GetAssemblingThreaInfo();
            listPoint = asInfo.GetAssemblingThreaPoint();
            for (int i = 0; i < listInfo.Count; i++)
            {
                FigureInfo figure = listInfo[i];
                figure.Location = listPoint[i];
                this.panMain.Controls.Add(figure);
            }
          
        }
        /// <summary>
        /// 随机生成新图形
        /// </summary>
        private void RandomNew()
        {
            fMove.isCanRemove();
            Random ra = new Random();
            int b = ra.Next(1, 5);
            switch (b)
            {
                case 1:
                    GetNewSqure();
                    break;
                case 2:
                    GetNewRectangle();
                    break;
                case 3:
                    GetNewAssemblingOne();
                    break;
                case 4:
                    GetNewAssemblingTwo();
                    break;
                case 5:
                    GetNewAssemblingThrea();
                    break;
            }
        }
        /// <summary>
        /// 向左移动
        /// </summary>
        private void MoveLeft()
        {
            fMove.listInfo = listInfo;
            List<Point> listLocation = fMove.MoveLeft();
            if (listLocation != null)
            {
                for (int i = 0; i < listLocation.Count; i++)
                {
                    listInfo[i].figurePoint = listLocation[i];
                    listInfo[i].Location = listLocation[i];
                }
            }
        }
        /// <summary>
        /// 向右移动
        /// </summary>
        private void MoveRight()
        {
            fMove.listInfo = listInfo;
            List<Point> listLocation = fMove.MoveRight();
            if (listLocation != null)
            {
                for (int i = 0; i < listLocation.Count; i++)
                {
                    listInfo[i].figurePoint = listLocation[i];
                    listInfo[i].Location = listLocation[i];
                }
            }
        }


       
    }
}
